package utils;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import app.ApplicationManager;
import app.security.Profile;
import app.security.ResSecLevel;

import com.vaadin.server.VaadinSession;

/**
 * Utilities to set and get attributes in a session
 * 
 * @author eduard
 * 
 */
public class SessionUtils {

	public static Object getSessionAttribute(String attName) {
		Object attValue = null;
		try {
			attValue = VaadinSession.getCurrent().getAttribute(attName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return attValue;
	}

	public static void setSessionAttribute(String attName, Object attValue) {
		try {
			VaadinSession.getCurrent().getLockInstance().lock();
			try {
				VaadinSession.getCurrent().setAttribute(attName, attValue);
			} finally {
				VaadinSession.getCurrent().getLockInstance().unlock();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Locale getSessionLocale() {
		Locale locale = null;
		try {
			locale = VaadinSession.getCurrent().getLocale();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return locale;
	}

	public static String getSessionUser() {
		String user = (String) getSessionAttribute("user");
		return user;
	}

	public static void setSessionUserAndApllications(String userName,String profile) {
		setSessionAttribute("user", userName);
		setSessionApplications(profile);
	}
	public static void setSessionUser(String userName) {
		setSessionAttribute("user", userName);
	}

	public static void setSessionLocale(Locale lc) {
		try {
			VaadinSession.getCurrent().setLocale(lc);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void setSessionNovice(boolean novice) {
		setSessionAttribute("novice", novice);
	}

	public static boolean getSessionNovice() {
		return (Boolean) getSessionAttribute("novice");
	}

	/**
	 * set list of all available applications
	 * 
	 * @param profile
	 */
	public static void setSessionApplications(String profile) {

		ApplicationManager appMngr = AppUtils.getApplicationManager();
		setSessionProfile(appMngr.getProf(profile));
		setSessionAttribute("AppList", appMngr.getAppsSecLevels(profile));

	}

	@SuppressWarnings("unchecked")
	public static List<ResSecLevel> getSessionApplications() {
		return (List<ResSecLevel>) getSessionAttribute("AppList");
	}

	@SuppressWarnings("unchecked")
	public static HashMap<String, Boolean> getSessionLoadedViews() {
		return (HashMap<String, Boolean>) getSessionAttribute("LoadedViews");
	}

	public static void setSessionLoadedViews(
			HashMap<String, Boolean> loadedViews) {
		setSessionAttribute("LoadedViews", loadedViews);
	}

	public static void setSessionCurrentApp(String appName) {
		setSessionAttribute("currentApp", appName);
	}

	public static String getSessionCurrentApp() {
		return (String) getSessionAttribute("currentApp");
	}

	public static void setSessionProfile(Profile profile) {
		setSessionAttribute("profile", profile);
	}

	public static Profile getSessionProfile() {
		return (Profile) getSessionAttribute("profile");
	}

	public static int getSessionAppSecLevel() {
		return getSessionProfile().getApps().get(getSessionCurrentApp());
	}

}
